var layer;
var form;
var table;
var upload;
var laydate;
var maxImg = 1;
var look = 0;
var columnObjName = "column_user";
var columnArray = ['id','name','sex','age','userName','useDate','phone','idcard','rname','oname','remarks'];
var interUrl = {
		select: $projectPath+'/sys/user/select',
		add: $projectPath+'/sys/user/add',
		edit: $projectPath+'/sys/user/edit',
		del: $projectPath+'/sys/user/delete',
		delBatch: $projectPath+'/sys/user/deleteBatch',
}

layui.use([ 'layer',  'form',  'table' ,  'laydate',  'upload' ],  function() {
	layer = layui.layer;
	form = layui.form;
	table = layui.table;
	upload = layui.upload;
	laydate = layui.laydate; 
	
	// 日期初始化
	laydate.render({elem : '#birthday', type:"date", format : "yyyy-MM-dd", trigger: 'click'});	
	laydate.render({elem : '#useDate', type:"datetime", format : "yyyy-MM-dd HH:mm:ss", trigger: 'click'});	
	
	// 加载字典
    getDictLabel("sex", "#sex", "sex", true);
    getDictLabel("sex", "#search_sex", "sex", false, 3);
	
    // 图片是否查看
    form.on('checkbox(maxImg)', function (data) {
        if (data.elem.checked) {
            maxImg = 1;
            localStorage.setItem("maxImg", "1");
        } else {
            maxImg = 0;
            layer.closeAll();
            localStorage.setItem("maxImg", "0");
        }
        return false;
    });
	
    // 上传图片
	upload.render({
	    elem: '#jzjzImg' 
	    , url: ''
	    , accept: 'images'
	    , exts: 'jpg|png|jpeg'
		, before: function(obj){
			if(look == 0){
				var idcard = $("#data_form").find("input[name='idcard']").val();
				this.url = $projectPath+'/upload/userImg?idcard='+idcard;
			}
        }
	    , done: function(res){
		  	layer.msg(res.msg, {offset:"10%"})
		  	if(res.success){
		  		$("#data_form").find("input[name='img']").val(res.data);
				$("#jzjzImg").attr("src",  $projectPath + res.data+"?v="+Math.random());
				setTimeout(function () {
					$("#jzjzImg").attr("src",  $projectPath + res.data+"?v="+Math.random());
				}, 2000)
		  	}
	    }
	    , error: function(){
	    	if(look == 1){
	    		layer.msg("当前为查看状态", {offset:"100px"})
	    	}else{
	    		layer.msg("上传接口异常", {offset:"100px"})
	    	}
	    }
	});
	
	// 执行一个 table 实例
	table.render({
		elem : '#data_table', 
		id : 'data_table', 
		url : interUrl.select, 
		title : '用户表', 
		height:'full-120', 
		//size:"sm", 
		page : true, 
		toolbar : "#toolberLeft", 
		cols : [[ 
			{type : 'checkbox', fixed : 'left'},  
			{field : 'id', title : '照片', align : 'center', width: '80', hide: setColumnHide("id"), 
				templet: function (d) {
	                var src = $projectPath+d.img+"?v="+Math.random();
	                var s = "onmouseenter='showImg(\""+d.img+"\", \"img_"+d.id+"\")'";
	                return '<img width="50" height="50" id="img_'+d.id+'" src="'+src+'" '+s+' onerror="javascript:this.src=\''+($projectPath+"/static/images/head.png")+'\'"/>';
				}
			},
			{field : 'name', title : '姓名', width: '150', align : 'center', hide: setColumnHide("name")}, 
			{field : 'sex', title : '性别', width: '80', sort : true, align : 'center', templet: (d) => getDictText("sex",d.sex), hide: setColumnHide("sex")}, 
			{field : 'age', title : '年龄', width: '80', sort : true, align : 'center', hide: setColumnHide("age")}, 
			{field : 'userName', title : '登录名称', width: '150', sort : true, align : 'center', hide: setColumnHide("loginName")}, 
			{field : 'useDate', title : '使用期限', width: '150', sort : true, align : 'center', hide: setColumnHide("useDate")}, 
			{field : 'phone', title : '手机号码', width: '150', sort : true, align : 'center', hide: setColumnHide("phone")}, 
			{field : 'idcard', title : '身份证号码', width: '150', sort : true, align : 'center', hide: setColumnHide("idcard")}, 
			{field : 'rname', title : '角色名称', width: '150', sort : true, align : 'center', hide: setColumnHide("rname")}, 
			{field : 'oname', title : '项目/组织', width: '150', sort : true, align : 'center', hide: setColumnHide("oname")}, 
			{field : 'remarks', align : 'center', width: '150', title : '备注', hide: setColumnHide("remarks")}, 
			{fixed : isRight(), align : 'center', width: '180', title : '操作', toolbar : '#toolRow'} 
		]]
		,done:function(result,currPage,count){
			// 初始化是否查看图片
			maxImg = localStorage.getItem("maxImg");
		    if(maxImg == '0'){
		    	$("#maxImg").attr("checked", false);
		    	form.render("checkbox");
		    }
	    }
	});
	
	// 头部左侧事件
	table.on('toolbar(data_table)',  function(obj) {
		if (obj.event === 'search') {
			openWindowT('search_form', "条件搜索", '100%', '150px', false, 0, false, false, null, 0.2, true)
		}else if(obj.event === 'add'){
        	var obj = {"birthday": getSysDate(1), "useDate": getSysDate(null, 70), "sex": 1}
			formData(obj, '添加用户');
		}else if(obj.event === 'delete'){
            deleteBatch(table, 'data_table', "id", interUrl.delBatch)
		}
	})
	
	// 行监听事件
	table.on('tool(data_table)',  function(obj) {
		var data = obj.data;
		if (obj.event === 'del') {
			deleteRow(table, 'data_table', data, interUrl.del);
		} else if (obj.event === 'edit') {
			formData(data, "编辑用户");
		} else if (obj.event === 'detail') {
			formData(data, "查看用户");
		}
	});
	
	// 查询
	form.on('submit(submitSearch)',  function(data) {
		reloadTable(table, 'data_table', data.field);
		return false;
	})
	
	// 提交
	form.on('submit(submitForm)',  function(data) {
		saveData(table, 'data_table', data.field, interUrl.add, '#data_form');
		return false;
	})
	// 加载所有角色
	loadDownSelector($projectPath+"/sys/role/selectAll", {}, 'rid,rid_search',  true,  'rid',  'rname');
	
})

/**
 * 表单赋值-------------------------------------------------------------------------------
 */
function formData(data, title) {
	look
	openWindowRB('data_form', title, '700px', '100%', false, 0, false, false, data, 0.2, false);
	if(data){
		$("#jzjzImg").attr('src', $projectPath+(data.img?data.img:"/static/images/head.png")+"?v="+Math.random());
	}
}

/**
 * 显示近照--------------------------------------------------------------------------------
 */
function showImg(img, id){
	if(maxImg > 0){
		var src = $projectPath+img+"?"+Math.random();
		layer.tips('<img src="'+src+'" style="width:180px;" onerror="javascript:this.src=\''+($projectPath+"/static/images/head.png")+'\'"/>',  '#'+id, {
	    	tips: [2, '#79c758']
	    });
	}
}














